﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using SharingCenter.Web.Classes.Profiles;
using SharingCenter.Web.Classes;
using System.IO;
using System.Runtime.Serialization.Json;
using SharingCenter.Web.Classes.Account;
using System.Web.Security;

namespace SharingCenter.Web.Services
{
    /// <summary>
    /// Summary description for UserServices
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class UserServices : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod]
        public string GetUserProfile(string userId)
        {
            Guid uid;
            try
            {
                uid = new Guid(userId);
            }
            catch { return string.Empty; }

            
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Sc_Profile));
            Sc_Profile pro =  ProfileOperations.GetProfile(uid);
            MemoryStream stream1 = new MemoryStream();
            ser.WriteObject(stream1, pro);
            stream1.Position = 0;
            StreamReader sr = new StreamReader(stream1);
            return sr.ReadToEnd();
        }

        [WebMethod]
        [ScriptMethod]
        public bool IsUserOnlineByName(string userName)
        {
            try
            {
                return Membership.GetUser(userName).IsOnline;
            }
            catch { return false; }
        }

        [WebMethod]
        [ScriptMethod]
        public bool IsUserOnlineById(string userId)
        {
            Guid uid;
            try
            {
                uid = new Guid(userId);
            }
            catch { return false; }

            try
            {
                return Membership.GetUser(AccountOperations.GetUserName(uid)).IsOnline;
            }
            catch { return false; }
        }

        [WebMethod]
        [ScriptMethod]
        public string GetUserInfo(string uidList)
        {
            List<string> ret = new List<string>();
            char[] s = { ';' };
            string[] tmp = uidList.Split(s);
            foreach (string u in tmp)
            {
                Guid uid = new Guid(u);
                Sc_Profile profile = ProfileOperations.GetProfile(uid);
                string imgPath = VirtualPathUtility.ToAbsolute(ProfileOperations.GetAvatarImagePath(uid, Classes.enums.AvatarType.Thumbnail, profile));
                int online = this.IsUserOnlineById(u) ? 1 : 0;
                ret.Add("{" + string.Format("'UserId':'{0}', 'NickName':\"{1}\", 'Thumb':'{2}', 'Online':{3}", u, profile.NickName, imgPath, online) + "}");
            }

            return "[" + String.Join(",", ret.ToArray()) + "]";
        }
    }
}
